package org.example.myframework.frame.demo.factory;

import org.example.myframework.frame.demo.factory.handler.PayHandler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工厂类，用于选择返回对应的策略对象
 */
@Component
public class PayFactory implements InitializingBean {

    @Resource
    private List<PayHandler> payHandlerList;

    private Map<PayEnum, PayHandler> payHandlerMap = new HashMap<>();


    @Override
    public void afterPropertiesSet() throws Exception {
        for (PayHandler payHandler : payHandlerList) {
            payHandlerMap.put(payHandler.getPayType(), payHandler);
        }
    }

    /**
     * 根据支付编码获取支付类型
     * @param payCode
     * @return
     */
    public PayHandler getPayHandlerByCode(Integer payCode) {
        PayEnum code = PayEnum.getByCode(payCode);
        return payHandlerMap.get(code);
    }
}
